package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.common.CustomException;
import xyz.weizhi.rjwm.common.baseContext;
import xyz.weizhi.rjwm.entity.ShoppingCart;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.ShoppingCartService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @Author milian
 * @Date 2021/9/29 0029 10:34
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCardController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> getCartList(){
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }
    @PostMapping("/add")
    public R<String> addCart(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        log.info("ShoppingCart:{}",shoppingCart);
        shoppingCart.setUserId((Long)request.getSession().getAttribute("user"));
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success("添加成功");
        }
        shoppingCartService.save(shoppingCart);
        return R.success("添加成功");
    }
    @DeleteMapping("/clean")
    public R<String> removeShoppingCart(){
        Long userID = baseContext.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userID);
        shoppingCartService.remove(lqw);
        return R.success("删除成功");
    }
    @Transactional
    @PostMapping("/sub")
    public R<String> cartNumberSubtract(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request){
        log.info("shoppingCart:{}",shoppingCart);
        Long userID = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userID);
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCart = shoppingCartService.getOne(lqw);
        Integer number = shoppingCart.getNumber();
        if (number==1){
            shoppingCartService.remove(lqw);
            return R.success("删除成功");
        }
        shoppingCart.setNumber(shoppingCart.getNumber()-1);
        shoppingCartService.updateById(shoppingCart);
        return R.success("删除成功");
    }
}
